// pages/register/register.js
const util = require('../../utils/util.js')
const api = require('../../config/api.js')

Page({
  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    cellPhoneNumber: '',
    photos: [], 

    showDialog: false,
    errorMsg: '',
  },
  onLoad: function (options) {

  },
  onReady: function () {
    
  },
  onShow: function () {
    this.setData({photos: wx.getStorageSync('photos')})
  },
  onHide: function () {

  },
  onUnload: function () {

  },
  bindNameInput: function(e) {
    this.setData({name: e.detail.value})
  },
  bindcellPhoneNumberInput: function(e) {
    this.setData({cellPhoneNumber: e.detail.value})
  },
  addFace: function() {
    if (!this.data.name) {
      this.setData({
        errorMsg: '姓名不能为空',
        showDialog: true
      })
      return
    }
    if (!this.data.cellPhoneNumber) {
      this.setData({
        errorMsg: '手机号不能为空',
        showDialog: true
      })
      return
    }
    if (this.data.photos.length == 0) {
      this.setData({
        errorMsg: '请先采集人脸',
        showDialog: true
      })
      return
    }
    const data = {
      'name': this.data.name,
      'cell_phone_number': this.data.cellPhoneNumber,
      'photos': this.data.photos
    }
    wx.showLoading({
      title: '保存中...',
    })
    util.request(api.UserFaceAdd, data, 'POST', 'application/json;charset=UTF-8').then((res)=> {
      wx.hideLoading()
      wx.showToast({
        title: '成功',
        icon: 'success',
        duration: 1000
      })
      console.log(res)
      this.resetForm()
    }, (error) => {
      wx.hideLoading()
      wx.showToast({
        title: '失败',
        icon: 'fail',
        duration: 1000
      })
    })
  },
  resetForm: function() {
    // this.data.photos.push('test')
    this.setData({
      name: '',
      cellPhoneNumber: '',
      photos: []
    })
    wx.setStorageSync('photos', [])
  },
  closeDialog: function() {
    this.setData({showDialog: false})
  },
})